//
// Created by zhulingfeng on 2020/3/6.
//

#include "binary_search_test.hpp"
#include <algorithm>
BinarySearchTest::BinarySearchTest() { Init(10, 20000); }
void BinarySearchTest::Init(int array_size, int num_arrays) {
  std::default_random_engine e;
  std::uniform_int_distribution<int> random_value(0, 10);
  std::uniform_int_distribution<unsigned> random_size(1, 100);

  sorted_vectors_.resize(num_arrays);
  search_targets_vectors_.resize(num_arrays);
  unique_sorted_vectors_.resize(num_arrays);
  for (int i = 0; i < num_arrays; ++i) {
    // int size = random_size(e);
    // Set sorted vectors.
    int size = array_size;
    auto& vector = sorted_vectors_[i];
    vector.resize(size);
    for (int j = 0; j < size; ++j) {
      vector[j] = random_value(e);
    }
    std::sort(vector.begin(), vector.end());

    // Set unique sorted vectors.
    auto& unique_vector = unique_sorted_vectors_[i];
    unique_vector = vector;
    auto unique_end = std::unique(unique_vector.begin(), unique_vector.end());
    unique_vector.erase(unique_end, unique_vector.end());

    // Set search targets
    auto& targets = search_targets_vectors_[i];
    targets.push_back(vector[0]);         // first value
    targets.push_back(vector[size - 1]);  // last value
    targets.push_back(unique_vector[0]);
    targets.push_back(unique_vector[unique_vector.size() - 1]);
    if (size > 2) {
      std::uniform_int_distribution<unsigned> random_index(1, size - 2);
      targets.push_back(random_index(e));  // random value
    }
    targets.push_back(random_value.max() + 1);  // value larger than any
    targets.push_back(random_value.min() - 1);  // value less than any
  }
  sorted_vectors_.emplace_back();
  search_targets_vectors_.emplace_back(0);
  unique_sorted_vectors_.emplace_back();

  assert(sorted_vectors_.size() == search_targets_vectors_.size());
  assert(sorted_vectors_.size() == unique_sorted_vectors_.size());
}
